package com.ruoyi.qiuyue.controller.qiuyue;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.qiuyue.common.JWTUtils;
import com.ruoyi.qiuyue.common.MusicConstat;
import com.ruoyi.qiuyue.domain.MusicFavorite;
import com.ruoyi.qiuyue.domain.MusicLove;
import com.ruoyi.qiuyue.domain.MusicSong;
import com.ruoyi.qiuyue.domain.MusicUser;
import com.ruoyi.qiuyue.service.IMusicLoveService;
import com.ruoyi.qiuyue.service.impl.MusicLoveServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 收藏控制器
 */
@RestController
@RequestMapping("/app/favorite")
public class QFavoriteController {

    @Resource
    private JWTUtils jwtUtils;
    @Autowired
    private IMusicLoveService musicLoveService;

    /**
     * 查看收藏
     * @return
     */
    @GetMapping("/my")
    public AjaxResult myFavorite(HttpServletRequest request,@RequestParam int pageNum,@RequestParam int pageSize) throws JsonProcessingException {
        MusicUser user = jwtUtils.getDataByJwtToken(request);
        List<MusicSong> musicLove = musicLoveService.getMusicLoveByUserId(user.getId(),pageNum,pageSize);
        Map<String,Object> data = new HashMap<>();
        data.put("favorite",musicLove);
        return AjaxResult.success().put("data",data);
    }

    /**
     * 添加收藏
     * @return
     */
    @GetMapping("/add")
    public AjaxResult addFavorite(HttpServletRequest request,@RequestParam Long id) throws JsonProcessingException {
        MusicUser user = jwtUtils.getDataByJwtToken(request);
        musicLoveService.addMusicLoveByUserAndMusicId(user,id);
        return AjaxResult.success(MusicConstat.SONG_ADD);
    }

    /**
     * 删除收藏
     * @return
     */
    @GetMapping("/delete")
    public AjaxResult deleteFavorite(HttpServletRequest request,@RequestParam Long id) throws JsonProcessingException {
        MusicUser user = jwtUtils.getDataByJwtToken(request);
        musicLoveService.deleteMusicLoveByUserAndMusicId(user,id);
        return AjaxResult.success(MusicConstat.SONG_DELETE);
    }
}
